import numpy as np

from FluidClass.FluidSettings import FluidSettings
from ResistanceClass.ResistanceAbstractClass import ResistanceAbstractClass as RAC
from math import pi, log10
from Utilities.MathFunctions import polynom


class StraightCircleFlowSharpSpikeRestriction(RAC):

    def __init__(self,
                 fluid: FluidSettings,
                 length: float,
                 diameter_inlet: float,
                 diameter_outlet: float,
                 index: int = None) -> None:
        super().__init__(fluid, length, index)
        self.diameter_inlet = diameter_inlet
        self.diameter_outlet = diameter_outlet
        self.type_name = 'Сужение внезапное'
        self.section = 'Круглое'


    def area(self) -> float:
        return pi * self.diameter_outlet ** 2 / 4

    def area_inlet(self) -> float:
        return pi * self.diameter_inlet ** 2 / 4

    def area_outlet(self) -> float:
        return pi * self.diameter_outlet ** 2 / 4

    def diameter_hydraulic(self) -> float:
        return self.diameter_outlet

    def local_resistance_coefficient(self) -> float:

        if self.reynolds_number() < 10:
            return 30 / self.reynolds_number()

        elif 10 <= self.reynolds_number() < 10000:

            re_database = [2, 2.5, 3, 4]
            lambda_database = [0.032, 0.034, 0.04, 0.04]
            re_database = np.array(re_database) * 10 ** 3
            return polynom(self.reynolds_number(), re_database, lambda_database, 4)

        elif self.reynolds_number() >= 10000:
            ratio = self.diameter_outlet ** 2 / self.diameter_inlet ** 2
            ratio_database = [0, 0.2, 0.4, 0.6, 0.8, 0.9, 1]
            a_database = [1, 0.85, 0.68, 0.503, 0.3, 0.178, 0]
            return 0.5 * polynom(ratio, ratio_database, a_database, 4)

    def resistance_coefficient_lambda(self) -> (str, float):

        if self.reynolds_number() < 2000:
            return 64 / self.reynolds_number()

        elif 2000 <= self.reynolds_number() <= 4000:
            re_database = [2, 2.5, 3, 4]
            lambda_database = [0.032, 0.034, 0.04, 0.04]
            re_database = np.array(re_database) * 10**3
            return polynom(self.reynolds_number(), re_database, lambda_database, 4)

        elif self.reynolds_number() > 4000:
            return 1 / ((1.8 * log10(self.reynolds_number()) - 1.64) ** 2)

    def get_params_geom(self):
        self.params_geom['Длина'] = str(self.length) + ' м'
        self.params_geom['Диаметр входной'] = str(self.diameter_inlet) + ' м'
        self.params_geom['Диаметр выходной'] = str(self.diameter_outlet) + ' м'
        return self.params_geom




